﻿using System;
using System.Data;
using System.Text;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.WebPage.Master;

namespace ZHDJ.Web
{
    public partial class MorePagePublicity : UseMorePageMasterPage
    {
        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["PageIndex"] ?? "";
            }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "";
            }
        }

        private string BranchID
        {
            get
            {
                return this.Request["BranchID"] ?? "";
            }


        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                hfType.Value = this.Request["type"] ?? "";
            }

            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                this.WriteList();
            }
            else
            {
                this.hfModule.Value = this.Module;
                if (this.Module == "VolunteerActivitie")
                {
                    if (this.Category == "")
                    {
                        this.hfTotalCount.Value = PartyVolunteerActivitie.GetActiviteTitle("").Tables[1].Rows[0][0].ToString();
                    }
                    else
                    {
                        this.hfTotalCount.Value = PartyVolunteerQuotation.GetQuotationTitle("").Tables[1].Rows[0][0].ToString();
                    }
                }
                else if (this.Module == "PartyFactor")
                {
                    this.hfTotalCount.Value = PartyArticle.GetArticleTitle(this.BranchID, this.Module, this.Category).Tables[1].Rows[0][0].ToString();
                    this.litpagination.Visible = false;
                }
                else if (this.Module == "JoinPartyPublicity")
                {
                    if (this.hfType.Value == "")
                    {
                        this.hfTotalCount.Value = partyjoinpublicity.GetListDataCountByType("JoinStep4");
                    }
                    else
                    {
                        this.hfTotalCount.Value = partyjoinpublicity.GetListDataCountByType(this.hfType.Value);
                    }

                }
                else if (this.Module == "RelationShippublicity")
                {
                  this.hfTotalCount.Value = PartyRelationnshipPublicity.GetListDataCount();
                }
                else if (this.Module == "PartyPayment")
                {
                    this.hfTotalCount.Value = PartyMemberCost.GetMemberCostCount(BranchID);
                }
                else
                {
                    this.hfTotalCount.Value = PartyArticle.GetArticleTitle(this.BranchID, this.Module, this.Category).Tables[1].Rows[0][0].ToString();
                }
            }
        }

        public void LoadData()
        {
            if (this.Module == "PartyFactor")
            {
                DataSet ds = PartyArticle.GetPartyArticleGridData(0, 1, this.Module, this.Category, "","");
                this.ltContent.Visible = true;
                this.ltContent.Text = ds.Tables[0].Rows[0]["Content"].ToString();
            }
        }

        private void WriteList()
        {
            string module = string.IsNullOrEmpty(hfModule.Value) ? this.Module : hfModule.Value;
            bool isTreasureBox = module == "TreasureBox";
            bool isDownloadLibrary = module == "PartySchoolOnline" && Category == "DownloadLibrary";
            bool isVolunteerActivitie = module == "VolunteerActivitie" && this.Category == "Call";
            bool isVolunteerQuotation = module == "VolunteerActivitie" && this.Category == "Quotation";
            bool isJoinPartyPublicity = module == "JoinPartyPublicity" && this.Category.IndexOf("publicity")> -1;
            bool isRelationShipPublicity = module == "RelationShippublicity" && this.Category == "relationpub";
            bool isPartyPayment = module == "PartyPayment" && this.Category == "Payment";

            StringBuilder sbHtml = new StringBuilder();
            DataTable dt = new DataTable();
            int startIndex = (Convert.ToInt32(this.PageIndex) - 1) * Convert.ToInt32(this.PageSize);
            if (isJoinPartyPublicity)
            {
                if (this.hfType.Value == "")
                {
                    dt = partyjoinpublicity.GetListDataByType(startIndex, Convert.ToInt32(PageSize), "JoinStep4").Tables[0];
                }
                else {
                    //根据分类获取
                    dt = partyjoinpublicity.GetListDataByType(startIndex, Convert.ToInt32(PageSize), this.hfType.Value).Tables[0];
                }


            }
            else if (isRelationShipPublicity) {
                dt = PartyRelationnshipPublicity.GetListData(startIndex, Convert.ToInt32(PageSize)).Tables[0];
            }
            else if (isPartyPayment)
            {
                dt = PartyMemberCost.GetMemberCost(startIndex, Convert.ToInt32(PageSize), this.BranchID).Tables[0];
            }
            else
            {
                dt = PartyArticle.GetMoreArticleTitle(Convert.ToInt32(PageSize), startIndex, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
            }
            if (isTreasureBox)
            {
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span><a href=\"" + Convert.ToString(item["Url"]) + "\" >下载</a></span>");
                    sbHtml.Append("<a target =\"_blank\"  href=\"" + Convert.ToString(item["Url"]) + "\" > " + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else if (isDownloadLibrary)
            {
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span><a target=\"_blank\" href=\"" + Convert.ToString(item["AuthorUrl"]) + "\" >下载</a></span>");
                    sbHtml.Append("<a target=\"_blank\" href=\"" + Convert.ToString(item["AuthorUrl"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else if (isVolunteerActivitie)
            {
                dt = PartyVolunteerActivitie.GetMoreActivitieTitle(Convert.ToInt32(PageSize), startIndex).Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd") + "</span>");
                    sbHtml.Append("<a target =\"_blank\"  href=\"Portal/VolunteerActivitie/VolunteerActiviteDetail.aspx?ID=" + Convert.ToString(item["ID"]) + "\" >[" + Convert.ToString(item["IsEndName"]) + "]" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else if (isVolunteerQuotation)
            {
                dt = PartyVolunteerQuotation.GetMoreQuotationTitle(Convert.ToInt32(PageSize), startIndex).Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd") + "</span>");
                    sbHtml.Append("<a target =\"_blank\"  href=\"Portal/VolunteerActivitie/VolunteerQuotationDetail.aspx?ID=" + Convert.ToString(item["ID"]) + "\" >" + Convert.ToString(item["Content"]) + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else if (isJoinPartyPublicity)
            {
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd HH:mm:ss") + "</span>");
                    switch (item["StepID"].ToString())
                    {
                        case "JoinStep4": sbHtml.Append("<a target =\"_blank\"  href=\"Portal/JoinParty/Activists.aspx?ID=" + Convert.ToString(item["ID"]) + "&AppllicationID=" + Convert.ToString(item["AppllicationID"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>"); break;
                        case "JoinStep12": sbHtml.Append("<a target =\"_blank\"  href=\"Portal/JoinParty/PotentialMember.aspx?ID=" + Convert.ToString(item["ID"]) + "&AppllicationID=" + Convert.ToString(item["AppllicationID"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>"); break;
                        case "JoinStep24": sbHtml.Append("<a target =\"_blank\"  href=\"Portal/JoinParty/ProbationaryMember.aspx?ID=" + Convert.ToString(item["ID"]) + "&AppllicationID=" + Convert.ToString(item["AppllicationID"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>"); break;
                        case "JoinStep36": sbHtml.Append("<a target =\"_blank\"  href=\"Portal/JoinParty/Regular.aspx?ID=" + Convert.ToString(item["ID"]) + "&AppllicationID=" + Convert.ToString(item["AppllicationID"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>"); break;
                    }

                    sbHtml.Append("</li>");
                }
            }
            else if (isRelationShipPublicity)
            {
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd HH:mm:ss") + "</span>");
                    sbHtml.Append("<a target =\"_blank\"  href=\"#\" >" + CutLongTitle(Convert.ToString(item["Title"]), 52) + "</a>"); 
                    sbHtml.Append("</li>");
                }
            }
            else if (isPartyPayment)
            {
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd HH:mm:ss") + "</span>");
                    sbHtml.Append("<a \" >" + Convert.ToString(item["UserName"]) + "缴纳" + Convert.ToString(item["Year"]) + "年" + Convert.ToString(item["Month"]) + "月党费" + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else
            {
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd HH:mm:ss") + "</span>");
                    sbHtml.Append("<a target =\"_blank\"  href=\"ArticleDetailContent.aspx?ID=" + Convert.ToString(item["ID"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>");
                    sbHtml.Append("</li>");
                }
            }

            Response.Clear();
            Response.Write(sbHtml.ToString());
            Response.End();
        }

        /// <summary>
        /// 截取标题
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string CutLongTitle(object obj, int length)
        {
            string title = Convert.ToString(obj);
            if (title.Length > length)
            {
                title = title.Substring(0, length) + "...";
            }
            return title;
        }
    }
}